﻿using FrameWork.Design;
using FrameWork.Plugin;
using FrameWork.Setting;
using MyServer.Common;
using MyServer.TcpIP;
using MyServer.Udp;
using ScreenMonitor.ClientLib.Plugin;
using ScreenMonitor.Common;
using ScreenMonitor.Common.File;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace ScreenMonitor.ClientLib
{
    public static class ClientMain
    {

        public static bool Init()
        {

            #region=======设定池初始化=======

            SettingPool.Init(new INISettingOperator(Environment.CurrentDirectory + @"\Setting.ini"));

            #endregion

            #region======全局变量初始化======
            
            CommandObjects.GZipDataCompressor = new GZipDataCompressor();
            
            ClientObjects.ClientGlobalSettingEditorItem=new SettingPropertyEditorItem(@"全局设定\客户端设定",null,typeof(ClientGlobalSetting));
            SettingPool.RegisterEditor(ClientObjects.ClientGlobalSettingEditorItem);
            SettingPool.LoadSetting();
            SettingPool.AcceptSetting();
            NetworkInterface Interface = NetworkInterfaceConverter.GetAdapter(ClientGlobalSetting.ClientBindAdapter);
            ClientGlobalSetting.ClientIpAddr = NetworkInterfaceConverter.GetIpV4Address(Interface);
            ClientGlobalSetting.ClientMulticastIpAddr = NetworkInterfaceConverter.GetMulticastIpV4Address(Interface);

            #endregion

            #region=======文件池初始化=======

            //=====对象初始化=====//
            FilePool.Init();
            FilePool.NeedToRequestEvent += FilePool_NeedToRequestEvent;

            //=====设定初始化=====//
            ClientObjects.FilePoolSettingEditorItem = new SettingPropertyEditorItem(@"全局设定\服务端对象\文件池", null, typeof(FilePool));
            SettingPool.RegisterEditor(ClientObjects.FilePoolSettingEditorItem);
            SettingPool.LoadSetting();
            SettingPool.AcceptSetting();

            #endregion

            #region======Tcp 相关初始化======

            CommandObjects.TcpCommandCenter = new TcpCommandCenter(CommandObjects.TcpCenter);
            CommandObjects.TcpCenter=new TcpIpCenter(
                new IPEndPoint(ClientGlobalSetting.ClientIpAddr,ClientGlobalSetting.ClientTcpPort),
                CommandObjects.TcpCommandCenter,
                CommandObjects.GZipDataCompressor);
            
            CommandObjects.TcpCommandCenter.OperateCommand += TcpCommandCenter_OperateCommand;
            
            //=====设定初始化=====//
            ClientObjects.TcpIpConnectManagerSettingEditorItem = new SettingPropertyEditorItem(@"TcpIP设定\连接管理器", CommandObjects.TcpCenter.ConnectionManager, CommandObjects.TcpCenter.ConnectionManager.GetType());
            ClientObjects.TcpIpDataSenderSettingEditorItem = new SettingPropertyEditorItem(@"TcpIP设定\数据发送器", CommandObjects.TcpCenter.Sender, CommandObjects.TcpCenter.Sender.GetType());
            ClientObjects.TcpIpDataReaderSettingEditorItem = new SettingPropertyEditorItem(@"TcpIP设定\数据接收器", CommandObjects.TcpCenter.Reader, CommandObjects.TcpCenter.Reader.GetType());
            ClientObjects.TcpIpDataOperaterSettingEditorItem = new SettingPropertyEditorItem(@"TcpIP设定\数据处理器", CommandObjects.TcpCenter.Operator, CommandObjects.TcpCenter.Operator.GetType());
            ClientObjects.TcpIpDataListenerSettingEditorItem = new SettingPropertyEditorItem(@"TcpIP设定\数据监听器", CommandObjects.TcpCenter.Listener, CommandObjects.TcpCenter.Listener.GetType());
            SettingPool.RegisterEditor(ClientObjects.TcpIpConnectManagerSettingEditorItem);
            SettingPool.RegisterEditor(ClientObjects.TcpIpDataSenderSettingEditorItem);
            SettingPool.RegisterEditor(ClientObjects.TcpIpDataReaderSettingEditorItem);
            SettingPool.RegisterEditor(ClientObjects.TcpIpDataOperaterSettingEditorItem);
            SettingPool.RegisterEditor(ClientObjects.TcpIpDataListenerSettingEditorItem);
            SettingPool.LoadSetting();
            SettingPool.AcceptSetting();

            #endregion

            #region======Udp 相关初始化======

 
            CommandObjects.UdpCommandCenter = new UdpCommandCenter(CommandObjects.UdpCenter);
            CommandObjects.UdpCenter = new UdpCenter(CommandObjects.UdpCommandCenter);  
            CommandObjects.UdpCommandCenter.OperateCommand += UdpCommandCenter_OperateCommand;
            CommandObjects.UdpCommandCenter.LocalUdpClient=new UdpClient(
                new IPEndPoint(ClientGlobalSetting.ClientIpAddr,ClientGlobalSetting.ClientUdpPort));

            //=====设定初始化=====//
            ClientObjects.UdpConnectManagerSettingEditorItem = new SettingPropertyEditorItem(@"Udp设定\连接管理器", CommandObjects.UdpCenter.ConnectionManager, CommandObjects.UdpCenter.ConnectionManager.GetType());
            ClientObjects.UdpDataSenderSettingEditorItem = new SettingPropertyEditorItem(@"Udp设定\数据发送器", CommandObjects.UdpCenter.Sender, CommandObjects.UdpCenter.Sender.GetType());
            ClientObjects.UdpDataReaderSettingEditorItem = new SettingPropertyEditorItem(@"Udp设定\数据接收器", CommandObjects.UdpCenter.Reader, CommandObjects.UdpCenter.Reader.GetType());
            ClientObjects.UdpDataOperaterSettingEditorItem = new SettingPropertyEditorItem(@"Udp设定\数据处理器", CommandObjects.UdpCenter.Operator, CommandObjects.UdpCenter.Operator.GetType());
            ClientObjects.UdpDataListenerSettingEditorItem = new SettingPropertyEditorItem(@"Udp设定\数据监听器", CommandObjects.UdpCenter.Listener, CommandObjects.UdpCenter.Listener.GetType());
            SettingPool.RegisterEditor(ClientObjects.UdpConnectManagerSettingEditorItem);
            SettingPool.RegisterEditor(ClientObjects.UdpDataSenderSettingEditorItem);
            SettingPool.RegisterEditor(ClientObjects.UdpDataReaderSettingEditorItem);
            SettingPool.RegisterEditor(ClientObjects.UdpDataOperaterSettingEditorItem);
            SettingPool.RegisterEditor(ClientObjects.UdpDataListenerSettingEditorItem);
            SettingPool.LoadSetting();
            SettingPool.AcceptSetting();

            #endregion

            #region======插件相关初始化======

            ClientPluginLoader.LoadPlugin(Environment.CurrentDirectory + @"\Plugins");

            foreach (IClientPlugin Plu in PluginPool.GetPlugins())
            {

                SettingPool.RegisterEditor(Plu.GetSettingEditors());
                SettingPool.LoadSetting();
                SettingPool.AcceptSetting();

                Plu.Init();
                Plu.Enable();

            }

            #endregion
            return true;
        }

        public static bool Start()
        {
            bool IsSuc =
                   CommandObjects.TcpCenter.Start() && 
                   CommandObjects.UdpCenter.Start();
            CommandObjects.UdpCommandCenter.SendCommand(CommandType.ClientStarted, new IPEndPoint(ClientGlobalSetting.ClientMulticastIpAddr, ClientGlobalSetting.ServerUdpPort));
            return true;
        }
        static void FilePool_NeedToRequestEvent(Common.File.FileRequestEventArgs e)
        {
            if (e.ResponceObject != null)
            {
                CommandObjects.TcpCommandCenter.SendCommandWithData(CommandType.SendFile, e.ResponceObject.ToByte(), (TcpClient)e.ResponceObject.Tag);
            }
        }

        static void UdpCommandCenter_OperateCommand(object sender, OperateCommandEventArgs e)
        {
            UdpCommandOperator.RunCommand(e.Package);
        }

        static void TcpCommandCenter_OperateCommand(object sender, OperateCommandEventArgs e)
        {
            TcpCommandOperator.RunCommand(e.Package);
        }

    }
}
